package com.wego.controller.manager;

import com.wego.controller.manager.base.BaseController;
import com.wego.service.base.BaseService;
import com.wego.common.bean.PageBean;
import com.wego.common.bean.ResultBean;
import com.wego.common.enumeration.ResultEnum;
import com.wego.common.minio.MinioUtil;
import com.wego.common.utils.QueryUtil;
import com.wego.common.utils.ResultBeanUtil;
import com.wego.converter.GoodsConverter;
import com.wego.entity.domain.Category;
import com.wego.entity.domain.Goods;
import com.wego.entity.query.GoodsQuery;
import com.wego.entity.vo.manager.GoodsVO;
import com.wego.service.CategoryService;
import com.wego.service.GoodsService;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品控制器
 *
 * @author hc
 */
@Controller
@RequestMapping("/manager/goods")
public class GoodsController extends BaseController<Goods> {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsConverter goodsConverter;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 不提供默认的构造方法，限制其子类必须调用该方法为baseService赋值
     * @param service
     */
    public GoodsController(BaseService<Goods> service) {
        super(service);
    }

    /**
     * 商品列表
     * @param goodsQuery
     * @param model
     * @param resultBean
     * @return
     */
    @GetMapping("/list")
    public String list(GoodsQuery goodsQuery, Model model, @ModelAttribute("resultBean") ResultBean resultBean) {
        //构造动态查询条件
        String keywords = goodsQuery.getKeywords();
        if (keywords != null) {
            if (goodsQuery.getName() == null) {
                goodsQuery.setName(keywords);
            }
            if (goodsQuery.getSellingPoint() == null) {
                goodsQuery.setSellingPoint(keywords);
            }
            if (goodsQuery.getInfo() == null) {
                goodsQuery.setInfo(keywords);
            }
        }

        PageBean<GoodsVO> pageBean = null;
        final PageBean<Goods> goodsPageBean = goodsService.selectPage(goodsQuery);
        //转换
        pageBean = goodsConverter.goodsPageBean2GoodsVOPageBean(goodsPageBean);

        //分页时携带上查询条件
        String params = QueryUtil.genQueryCondition(goodsQuery);
        pageBean.setUrl("manager/goods/list?" + params);
        model.addAttribute("pageBean", pageBean);

        //查询条件数据回显时使用
        model.addAttribute("goodsQuery", goodsQuery);

        //增删改操作完返回到列表页面的提示信息
        if (resultBean != null) {
            model.addAttribute("resultBean", resultBean);
        }

        return "manager/goods";
    }

    /**
     * 打开添加修改页面
     * @param id
     * @param model
     * @return
     */
    @Override
    @GetMapping("/openAddUpdate")
    public String openAddUpdate(Long id, Model model) {
        //获取全部顶级商品类别
        final List<Category> categoryList = categoryService.selectCategoryByPid(null, -1, false);
        model.addAttribute("categoryList", categoryList);

        if (id != null) {
            Goods goods = goodsService.selectByPrimaryKey(id);
            model.addAttribute("goods", goods);
        }

        return "manager/goods_add_update";
    }

    /**
     * 具体执行添加修改操作
     * @param goods
     * @return
     */
    @PostMapping("/doAddUpdate")
    public String doAddUpdate(Goods goods, MultipartFile picFile, MultipartFile[] imgsFile, Model model) {
        //上传图片
        if (picFile != null && picFile.getSize() > 0) {
            final String picFileName = minioUtil.putObject2(picFile, "goods", null);
            goods.setPic(picFileName);
        }

        if (imgsFile != null && imgsFile.length > 0) {
            String imgs = "";
            for (MultipartFile item : imgsFile) {
                final String tmp = minioUtil.putObject2(item, "goods", null);
                imgs += tmp + ",";
            }
            goods.setImgs(imgs.substring(0, imgs.length() - 1));
        }
        goods.setUpdateTime(LocalDateTime.now());

        ResultBean resultBean;
        Long id = goods.getId();
        if (id != null) {
            //修改
            boolean res = goodsService.updateByPrimaryKeySelective(goods);
            resultBean = ResultBeanUtil.build(res, ResultEnum.UPDATE_SUCCESS, ResultEnum.UPDATE_EXCEPTION);
        } else {
            //添加
            goods.setCreateTime(LocalDateTime.now());
            boolean res = goodsService.insertSelective(goods);
            resultBean = ResultBeanUtil.build(res, ResultEnum.INSERT_SUCCESS, ResultEnum.INSERT_EXCEPTION);
        }

        //返回列表页面时，对话框中显示的提示信息
        if (resultBean != null) {
            model.addAttribute("resultBean", resultBean);
        }

        //重定向
        return "redirect:list";
    }

}
